from rest_framework import serializers

from api.models import Basket, BasketLine, Porduct, PorductImage
from api.models.user import ShipAddress

from api.utils.enums import BasketType, PriceType

from api.utils.serializers import PaginationSerializer, PaginationResultSerializer


class DeleteBasketLineBodySerializer(serializers.Serializer):
    product_ids = serializers.CharField(help_text="商品ID", max_length=256, required=True)

    def validate_product_ids(self, value):
        if value:
            value = value.split(",")
        return value


class BasketLineQeurySerializer(serializers.Serializer):

    product_id = serializers.IntegerField(help_text="商品ID", required=False)
    quantity = serializers.IntegerField(help_text="数量", required=False)
    pay_type = serializers.ChoiceField(
        help_text="支付类型", choices=PriceType.to_choices(), default=PriceType.RMB.value
    )
    status = serializers.ChoiceField(
        help_text="购物车类型", choices=BasketType.to_choices(), default=BasketType.PRE.value
    )
    address = serializers.IntegerField(help_text="地址ID", required=False)
    choice = serializers.BooleanField(help_text="是否选择商品", default=True)

    def validate_address(self, address):
        try:
            self.address = ShipAddress.objects.get(id=address)
        except Exception:
            return self.address
        return self.address


class BasketLineIdSerializer(serializers.ModelSerializer):
    class Meta:
        model = Basket
        fields = ["id"]


class BasketIdSerializer(serializers.ModelSerializer):
    class Meta:
        model = Basket
        fields = ["id"]


class BasketProductImagesSerializer(serializers.ModelSerializer):
    class Meta:
        model = PorductImage
        fields = "__all__"


class BasketProductBodySerializer(serializers.ModelSerializer):
    images = BasketProductImagesSerializer(many=True)
    videos = BasketProductImagesSerializer(many=True)

    class Meta:
        model = Porduct
        fields = "__all__"


class BasketLineBodySerializer(serializers.ModelSerializer):
    product = BasketProductBodySerializer()

    class Meta:
        model = BasketLine
        fields = "__all__"


class BasketBodySerializer(serializers.ModelSerializer):
    basket_lines = BasketLineBodySerializer(many=True)

    class Meta:
        model = Basket
        fields = [
            "id",
            "status",
            "date_merged",
            "date_submitted",
            "total_rmb",
            "total_point",
            "basket_lines",
        ]


class BasketDataSerializer(PaginationResultSerializer):
    data = BasketBodySerializer(many=True)


class BasketQeuryListSerializer(PaginationSerializer):
    username = serializers.CharField(help_text="用户名", max_length=256, default="")

    status = serializers.ChoiceField(help_text="订单类型", choices=BasketType.to_choices(), default="")
    product = serializers.IntegerField(help_text="商品ID", required=False)
    pay_type = serializers.ChoiceField(help_text="支付类型", choices=PriceType.to_choices(), default="")
